#region Using directives
using System;
using System.Collections.Generic;
#endregion

namespace SharpWind.States
{
	public class StateManager
	{
		#region Fields

		private Dictionary<string, State> states = new Dictionary<string, State>();

		private State currentState = null;

		#endregion

		#region Methods

		public void Add( State state )
		{
			Add( state, false );
		}

		public void Add( State state, bool change )
		{
			this.states.Add( state.Name, state );

			if( change )
			{
				if( this.currentState != null )
				{
					this.currentState.Close();
				}

				this.currentState = this.states[state.Name];
				this.currentState.Load();
			}
		}

		public void Remove( State state )
		{
			Remove( state.Name );
		}

		public void Remove( string name )
		{
			if( !string.IsNullOrEmpty( name ) && this.states.ContainsKey( name ) )
			{
				this.states.Remove( name );
			}
		}

		public void ChangeTo( string name )
		{
			if( !string.IsNullOrEmpty( name ) && this.states.ContainsKey( name ) )
			{
				if( this.currentState != null )
				{
					this.currentState.Close();
				}

				this.currentState = this.states[name];
				this.currentState.Load();
			}
		}

		#endregion

		#region Properties

		public State CurrentState
		{
			get
			{
				return this.currentState;
			}
		}

		#endregion
	}
}
